class PlayerSpellCaster extends GameCharacter
{	
	private var kaboomSound:AudioClip;	

	public function PlayerSpellCaster()
	{		
		super();

		characterName = "Spellcaster";
		
		//Attack
		attackRadius = 6;
		attackDamage = 30;
		actualDamage = 30;
		_attackPercent = 50.0;
		
		//Sight
		sightRadius = 7;
		moveRadius = 5;
		
		//Health Status
		maxHealth = 100;
		myHealth = maxHealth;
		
		//Skills
		_maxSkillCooldown = 2;
		skillRadius = 2;
		_maxSkillCooldown = 3;
		
		//For diplaying mouse over
		healthPercent = 100.0f;
		healthBarLength = Screen.width/9;
		style = new GUIStyle();
	    texture = new Texture2D(1, 1);
		
		//Enemy type
		_playerType = PlayerType.PLAYER_SPELL;
		
		buttonSkillTexture = Resources.Load("Buttons/kaboom_buttonNormal");
	 	buttonSkillHoverTexture = Resources.Load("Buttons/kaboom_buttonHover");
	 	buttonSkillPressedTexture = Resources.Load("Buttons/kaboom_buttonPressed");
	 	
	 	skillstyle = new GUIStyle();
		skillstyle.normal.background = buttonSkillTexture;
		skillstyle.hover.background = buttonSkillHoverTexture;
		skillstyle.active.background = buttonSkillPressedTexture;		
		
		selectedSound = Resources.Load("spellcasterSound");	
		kaboomSound = Resources.Load("kaboomSound");			
	}
	
	public function attack(target:EnemyCharacter, projectileType:int)
	{
		super.attack(target, projectileType);
		
		//calcualte hit chance
		var hitChance = calculateHitChance(target) + attackChanceModifier;
		
		//fire bullet
		var bullet = Resources.Load("Projectile_spell");
		bullet = Instantiate(bullet);
		var projectile = bullet.GetComponent(Projectile);
		
		var chance = Random.Range(0,100);
		
		//random attack chance
		if (chance < hitChance){
			//hit
			projectile.setup(this, target, true, projectileType);
			attackChanceModifier = 0;
		}else{
			//miss
			projectile.setup(this, target, false, projectileType);
			attackChanceModifier += Mathf.Floor((chance - hitChance)/5.0);
		}	
		
		Destroy(bullet,5);
		endTurn();
	}
	
	//OVERRIDE THIS FUNCTION IN EVERY INHERITED CLASS
	public function calculateHitChance(target:EnemyCharacter)
	{
		//currently calculating based on an average atk
		//calculate distance of enemy
		var distance = Vector3.Distance(target.getSurfaceCoord(), this.getSurfaceCoord());
		//based on the maximum range of weapon, calculate the hit chance based on the optimized range
		//close - mid range weapon
		var optimizedRangeMax = 5;
		var hitChance = 100;
		//too far
		if (distance > optimizedRangeMax)
			hitChance -= (distance - optimizedRangeMax) * 20;
		//if in cover
		if (target.isInCover())
		{
			if (!target.isFlanked(this))
				hitChance -= Obstacle.HIT_CHANCE_REDUCTION;
		}
		
		//check height advantage
		var heightDifference = target.getSurfaceCoord().y - this.getSurfaceCoord().y;
		if (heightDifference > 0)
			hitChance -= heightDifference * 10;
		else if (heightDifference < 0)
			hitChance += heightDifference * -10;
		
		//minimum 10%
		if (hitChance < 10)
			hitChance = 10;
		else if (hitChance > 100)
			hitChance = 100;
		return hitChance;
	}
	
	public function calculateActualDamage(target:EnemyCharacter)
	{
		//if in cover
		if (target.isInCover())
		{
			//check whether flanked
			if (target.isFlanked(this))
			{
				return attackDamage *2; 
			}
			else
			{
				return attackDamage;
			}
		}
		//not in cover
		else
		{
			return attackDamage *2;
		}
	}
	
	public function skill(target:EnemyCharacter) // Spell attack
	{
		if(!_isCooldown){
			setAnimationState(SKILL_ANI);
			startCooldown();
			
			super.attack(target, Projectile.NORMAL);
			
			var playersInSkillRange:Array = _mapReference.getTileCharacters(target.getCoord(), skillRadius, false); 
										
			//fire bullet
			var bullet = Resources.Load("Projectile_meteor");
			bullet = Instantiate(bullet);
			var projectile = bullet.GetComponent(Projectile);
			
			audio.PlayClipAtPoint(kaboomSound,Vector3(0,0,0));				

			projectile.setup(this, target, true, Projectile.METEOR);
			
			for(var i:int=0; i<playersInSkillRange.length; i++){
				var current_player = playersInSkillRange[i];
				
				if(current_player != target)
					current_player.adjustCurrentHealth(-getActualDamage());
			}
			attackChanceModifier = 0;
			
			Destroy(bullet,5);
			
			endTurn();
		}
	}

}